import { Component,OnInit, Input } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import {Headers,RequestOptions,URLSearchParams} from "@angular/http";
import { Location } from '@angular/common';
import {User} from "./umoudel/user";
import {Uinfo} from "./umoudel/uinfo";
import {UserService} from "./uservice/user.service";
import {PagerService} from "../../pagination/pager.service";
import {UtilService} from "../pollutmanage/util.service";


@Component({
  selector: 'app-code',
  templateUrl: './code.component.html',
  styleUrls: ['./code.component.css']
})
export class CodeComponent implements OnInit {
  lookInfo:User = new Uinfo("","","","","","","");//查看详情信息
  query:User;//查询传参
  addInfo:User;
  pageInfo = {
    totalItems: 0,
    currentPage: 1,
    pageSize: 10
  };
  dataList:any;
  pchecked:boolean = false;
  isRemove:boolean = true;//点击移除删除弹框
  errorMessage:'错误';//错误信息
  deleteId:'';//删除id
  userType:'';
  roleType:'';
  adduserId:'';
  addroleId:'';
  constructor(private http: HttpClient,
              location:Location,
              private userService: UserService,
              private pagerService: PagerService,
              private utilService: UtilService) {}

  getUserType() {
    this.userService.getUserType()
      .subscribe(
        data => {
          this.userType=data;
        });
  }
  getRoleType() {
    this.userService.getRoleType()
      .subscribe(
        data => {
          this.roleType=data;
        });
  }
///查看and编辑
  userMessage(polluter:any){
    let lookObj = {};
    Object.assign(lookObj, polluter);
    this.lookInfo = lookObj;
  }

//查询
  doSearch(query) {
    this.query = query;
    this.pageInfo.currentPage = 1;
    this.getPageData();
  }
  setPage(toPageNum) {
    this.pageInfo.currentPage = toPageNum;
    this.getPageData();
  }
  //污染源列表
  getPageData() {
    let queryObj = {};
    Object.assign(queryObj, this.query,{
      limit: this.pageInfo.pageSize,
      current: this.pageInfo.currentPage
    });
    const queryStr = this.utilService.toFormData(queryObj);
    this.getExhibits(queryStr);
  }
  getExhibits(queryStr) {
    this.userService.getExhibits(queryStr).subscribe(data => {
        this.dataList = data.records;
        this.pageInfo.totalItems = data.total;
      },
      error => this.errorMessage = <any>error
    );
  }
  //删除
  delete(pollutId) {
    this.deleteId=pollutId;
  }
  deletePollut(id) {
    this.userService.deletePollut(id)
      .subscribe(
        data => {
          if(data.resultCode==0) {
            $("#delete").modal('show');
            this.getPageData();
          }else {
            alert("删除失败！");
          }
        });
  }
  //添加
  doAdd(addInfo:any,valid:boolean){
    if(valid) {
      this.userService.createPollut(addInfo).subscribe(
        data => {
          if (data.resultCode == 0) {
            $("#addModal").modal('hide');
            this.adduserId='';
            this.addroleId='';
            $("#add").modal('show');
            this.getPageData();
          } else {
            alert("添加失败！");
          }
        });
    }
  }
//编辑
  doEdit(editInfo:any,valid:boolean){
    if(valid) {
      this.userService.editExhibit(editInfo).subscribe(
        data => {
          if (data.resultCode == 0) {
            $("#updateModal").modal('hide');
            $("#update").modal('show');
            this.getPageData();
          } else {
            alert("编辑失败！");
          }
        });
    }
  }
  ngOnInit(){
    //获取分页信息
    this.getPageData();
    this.getUserType();
    this.getRoleType();
  }

}
